#include "CGUIProgressBar.h"
namespace irr 
{
    namespace gui 
    {
        CGUIProgressBar::CGUIProgressBar(bool horizontal, bool border, bool background, IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
            : IGUIProgressBar(horizontal, border, background, environment, parent, id, rectangle)
        {
            dHorizontal = horizontal;
            dBorder = border;
            dBackground = background;
            setMax();
            setPos();
            setFontText();
            setColorBack();
            setColorFront();
            setColorText();
        }

        CGUIProgressBar::~CGUIProgressBar()
        {
        }

        void CGUIProgressBar::setMax(s32 m)
        {
            pMax = m;
            if(pMax < 1) pMax = 1;
        }

        void CGUIProgressBar::setPos(s32 p)
        {
            pPos = p;
            if(pPos < 0) pPos = 0;
            if(pPos > pMax) pPos = pMax;
        }

        void CGUIProgressBar::setColorBack(video::SColor color)
        {
            cBack = color;
        }

        void CGUIProgressBar::setColorFront(video::SColor color)
        {
            cFront = color;
        }

        void CGUIProgressBar::setColorText(video::SColor color)
        {
            cText = color;
        }

        void CGUIProgressBar::setFontText(IGUIFont* font)
        {
            oFont = font;
        }

        void CGUIProgressBar::draw()
        {
            if(!IsVisible) return;
            static IGUISkin* skin = Environment->getSkin();
            if(!skin) return;
            static irr::video::IVideoDriver* driver = Environment->getVideoDriver();
            if(!driver) return;
            core::rect<s32> frameRect(AbsoluteRect);
            if(dBackground)
                driver->draw2DRectangle(cBack, frameRect, &AbsoluteClippingRect);
            if(dBorder){
                skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
                frameRect.UpperLeftCorner.X += 1;
                frameRect.UpperLeftCorner.Y += 1;
                frameRect.LowerRightCorner.X -= 1;
                frameRect.LowerRightCorner.Y -= 1;
            }
            core::rect<s32> progRect(frameRect);
            if(dHorizontal)
                progRect.LowerRightCorner.X = progRect.UpperLeftCorner.X + (pPos * (frameRect.LowerRightCorner.X - frameRect.UpperLeftCorner.X) / pMax);
            else
                progRect.UpperLeftCorner.Y = progRect.LowerRightCorner.Y - (pPos * (frameRect.LowerRightCorner.Y - frameRect.UpperLeftCorner.Y) / pMax);
            driver->draw2DRectangle(cFront, progRect, &AbsoluteClippingRect);
            if(Text.size()){
                if(!oFont) oFont = skin->getFont();
                if(oFont){
                    frameRect.UpperLeftCorner.X += ((frameRect.getWidth() - oFont->getDimension(Text.c_str()).Width) / 2);
                    oFont->draw(Text.c_str(), frameRect, cText, false, true, &AbsoluteClippingRect);
                }
            }
            IGUIElement::draw();
        }
    }
}
